package org.yh.baiduyun;

import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * @Author: yh
 * @Description:
 * @Date: Created in 11:44 2019/3/15
 * @Modified By:
 */
public class TextAnalysis {

    public static void main(String[] args) throws UnsupportedEncodingException {
        //String auth = AuthToken.getAuth();
        //System.out.print("结果：" + auth);
        //String text = "味道很不错，很喜欢吃。服务也很好感觉很亲切，吃的很舒服，谢谢";
        //text = URLEncoder.encode(text, "GBK");
        //String s = commentTag(text, auth);
        //System.out.println("结果" + JSONObject.parse(s));
        //String t = JSONObject.parseObject(s).getString("text");
        System.out.println("解码后："+URLDecoder.decode("%CE%B6%B5%C0%BA%DC%B2%BB%B4%ED%A3%AC%BA%DC%CF%B2%BB%B6%B3%D4"));
    }

    public static String toGBK(String source) throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        byte[] bytes = source.getBytes("GBK");
        for (byte b : bytes) {
            sb.append("%" + Integer.toHexString((b & 0xff)).toUpperCase());
        }

        return sb.toString();
    }

    public static String commentTag(String text, String token) {
        String requestIp = "https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?access_token=" + token;
        HashMap<String, String> map = new HashMap<>();
        map.put("Content-Type", "application/json");
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("text", text);
        //hash.put("type", 11);
        String s = LiangChaoHttp.sendPost(requestIp, hash);
        return s;
    }

}
